﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using System.Collections.Generic;
using System.Globalization;
using Jewelry.Entities;

public partial class Pages_USearchProducts : Mgh.UI.Sql.PageBase
{
    protected CultureInfo myInfo;
    ProductBLL bn;
    protected string ImgPath;

    private DataSources.Jw_Tbl_CartDetailDataTable Cart
    {
        get { return Session["cart"] == null ? null : Session["cart"] as DataSources.Jw_Tbl_CartDetailDataTable; }
        set { Session["cart"] = value; }
    }

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        myInfo = CultureInfo.InvariantCulture;
        if (!IsPostBack)
        {
            this.includeCss();
            this.includeScript();
            if (string.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                CategoryBLL bn = new CategoryBLL(getCurrentConnect());
                DataSources.Jw_Tbl_CategoryDataTable tb = bn.getCategoriesOnTop(1);
                if (tb != null && tb.Count > 0)
                {
                    Response.Redirect("USearchProducts.aspx?cid=" + tb[0].CateID);
                }
            }
            ImgPath = ResolveUrl("~/Upload/");
        }
    }

    protected void odsCateCreating(object sender, ObjectDataSourceEventArgs e)
    {
        bn = new ProductBLL(getCurrentConnect());
        e.ObjectInstance = bn;
    }

    protected void odsCateSelected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue == null)
        {
            //cval.IsValid = false;
            //cval.Text = bn.getMessage();
        }
    }

    protected void odsCateSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        List<object> list = new List<object>();
        e.InputParameters["listFeature"] = list;
        e.InputParameters["strStatus"] = "Enable";
    }

    protected void listData_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("cmdBuy"))
        {
            if (CurrentUser.checkUser())
            {
                CartBLL bnC = new CartBLL(getCurrentConnect());
                DataSources.Jw_Tbl_CartDetailDataTable tb = new DataSources.Jw_Tbl_CartDetailDataTable();
                DataSources.Jw_Tbl_CartDetailRow rw = tb.NewJw_Tbl_CartDetailRow();
                rw.DeID = string.Empty;
                rw.ProID = e.CommandArgument.ToString();
                rw.SoLuong = 1;
                rw.Username = CurrentUser.Username;
                tb.AddJw_Tbl_CartDetailRow(rw);
                if (!bnC.addCartDetails(CurrentUser.Username,tb))
                {
                    cval.IsValid = false;
                    cval.Text = "Hiện tại website đang bảo trì, xin quí quay lại sau.";
                }
            }
            else
            {
                Response.Redirect("ULogin.aspx?url=" + HttpUtility.UrlEncode(Request.Url.PathAndQuery));
            }
        }
        else { }
    }

    #region functions

    protected void includeScript()
    {
        ltScript.Text = "<script language=\"javascript\" type=\"text/javascript\" src=\"~/Scripts/jquery.timers-1.2.js\"></script>";
        ltScript.Text = "<script language=\"javascript\" type=\"text/javascript\" src=\"~/Scripts/jquery.easing.1.3.js\"></script>";
        ltScript.Text = "<script language=\"javascript\" type=\"text/javascript\" src=\"~/Scripts/jquery.galleryview-3.0-dev.js\"></script>";
    }

    protected void includeCss()
    {
        ltCss.Text = "";
        ltCss.Text = "<link href=\"/Stylesheet/jquery.galleryview-3.0-dev.css\" rel=\"Stylesheet\" type=\"text/css\" rel='stylesheet' media='screen'/><br/>";
    }

    #endregion

}
